home *** CD-ROM | disk | FTP | other *** search
/ SuperHack / SuperHack CD.bin / CODING / DELPHI / CODEAPP.ZIP / MAINFORM.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1995-12-21  |  10.0 KB  |  374 lines

  1. unit Mainform;
  2. (*-----
  3.     File: MAINFORM.PAS for Project CODEAPP.DPR
  4.    Description:
  5.   Application to select a code file for viewing or copy and pasting
  6.   code sections from the file, or printing the file.
  7.   For text (ASCII) files only.
  8.  
  9. -----*)
  10.  
  11. (* uncomment this to enable DDE
  12.  {$DEFINE Using DDE} {enables DDE}
  13. *)
  14.  
  15. interface
  16.  
  17. uses
  18.   SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
  19.   Forms, Dialogs, StdCtrls, FileCtrl,
  20.   IniFiles, Printers, Grids, Buttons, ExtCtrls, ShellAPI,
  21.   Viewcode, FindWhat, PrintDlg, FileDlg, FileFunc, TextClip, DdeMan;
  22.  
  23. type
  24.   TFileForm = class(TForm)
  25.     BtnExit: TBitBtn;
  26.     BtnHelp: TBitBtn;
  27.     ViewBtn: TBitBtn;
  28.     PrintBtn: TBitBtn;
  29.     Panel1: TPanel;
  30.     FilesName: TLabel;
  31.     FileInfo: TLabel;
  32.     UpdateBox: TCheckBox;
  33.     SelectFileBtn: TBitBtn;
  34.     DdeClientConv1: TDdeClientConv;
  35.     DdeClientItem1: TDdeClientItem;
  36.     DDELinkBtn: TBitBtn;
  37.     procedure ViewBtnClick(Sender: TObject);
  38.     procedure PrintBtnClick(Sender: TObject);
  39.     procedure BtnExitClick(Sender: TObject);
  40.     procedure BtnHelpClick(Sender: TObject);
  41.     procedure FormClose(Sender: TObject; var Action: TCloseAction);
  42.     procedure FormShow(Sender: TObject);
  43.     procedure FormCreate(Sender: TObject);
  44.     procedure SelectFileBtnClick(Sender: TObject);
  45.     procedure FormActivate(Sender: TObject);
  46.     procedure FormDragDrop(Sender, Source: TObject; X, Y: Integer);
  47.     procedure FormDragOver(Sender, Source: TObject; X, Y: Integer;
  48.       State: TDragState; var Accept: Boolean);
  49.     procedure DdeClientItem1Change(Sender: TObject);
  50.     procedure DDELinkBtnClick(Sender: TObject);
  51.   private
  52.     { Private declarations }
  53.     printing : boolean;
  54.     theFile: string; {name of file}
  55.     FDateStr: string; {file's date string}
  56.     procedure WndProc(var Msg: TMessage); override;
  57.     procedure ShowSelection(const FN: string);
  58.     procedure CheckCmdLine;
  59.   public
  60.     { Public declarations }
  61.   end;
  62.  
  63. var
  64.   FileForm: TFileForm;
  65.  
  66. implementation
  67.  
  68. {$R *.DFM}
  69.  
  70. procedure TFileForm.ShowSelection(const FN: string);
  71. {-Show info on file selected or dropped on form}
  72. var
  73.   F: TSearchRec;
  74. begin
  75.   FilesName.Caption := ExtractFilePath(FN);
  76.   if GetFileInfo(FN, F) then
  77.   begin
  78.     FDateStr := FormatDateTime(DateTimeFormat,
  79.     FileDateToDateTime(F.Time));
  80.     FileInfo.Caption := ShowFileStats(F);
  81.     ViewBtn.Enabled := True;
  82.     PrintBtn.Enabled := True;
  83.   end
  84.   else
  85.   begin
  86.     FDateStr := '';
  87.     FileInfo.Caption := 'FILE INFO UNAVAILABLE';
  88.     theFile := '';
  89.   end;
  90. end;
  91.  
  92. procedure TFileForm.BtnExitClick(Sender: TObject);
  93. {-End this App}
  94. begin
  95.   Close
  96. end;
  97.  
  98. procedure TFileForm.BtnHelpClick(Sender: TObject);
  99. {-Tell user basic use}
  100. begin
  101.   MessageDlg('Select a File by dragging it from another App'+#13+
  102.   'and dropping it on this form or '+#13+
  103.   'use the Select File Dialog.',
  104.   mtInformation, [mbCancel], 0);
  105. end;
  106.  
  107. procedure TFileForm.FormCreate(Sender: TObject);
  108. {-Set up main form}
  109. begin
  110.   DragAcceptFiles(handle, True);
  111.   ViewBtn.Enabled := False;
  112.   PrintBtn.Enabled := False;
  113.   Top := 0;
  114.   Left := 0;
  115.   {$IFDEF Using DDE}
  116.   DDELinkBtn.Visible := True;
  117.   {$ELSE}
  118.   DDELinkBtn.Visible := False;
  119.   {$ENDIF}
  120. end;
  121.  
  122. procedure TFileForm.SelectFileBtnClick(Sender: TObject);
  123. begin
  124.   SelectFileDlg.FileSpec := theFile;
  125.   SelectFileDlg.Show
  126. end;
  127.  
  128. procedure TFileForm.FormActivate(Sender: TObject);
  129. {-Show selected file}
  130. begin
  131.   if SelectFileDlg.FileSpec <> '' then
  132.   begin
  133.     theFile := SelectFileDlg.FileSpec;
  134.     ShowSelection(theFile);
  135.   end
  136. end;
  137.  
  138. procedure TFileForm.ViewBtnClick(Sender: TObject);
  139. {-Exec the text viewer for file selected}
  140. begin
  141.   if theFile = '' then
  142.     MessageBeep(0)
  143.   else
  144.     with ViewText do
  145.     begin
  146.       LoadFile(theFile, FDateStr);
  147.       theTarget := ''; {let user choose new}
  148.     end
  149. end;
  150.  
  151. procedure TFileForm.PrintBtnClick(Sender: TObject);
  152. {-Set up print options, then print file}
  153. begin
  154.   with PRNformatDlg do
  155.   begin
  156.     if printing then {quit}
  157.     begin
  158.       pcancel := True;
  159.       exit;
  160.     end;
  161.     TextList := TStringList.Create;
  162.     try
  163.       TextList.LoadFromFile(theFile);
  164.       SetPrintFactors;
  165.       if ShowModal <> mrCancel then {do it}
  166.       begin
  167.         PrintBtn.Caption := 'Cancel';
  168.         pcancel := False;
  169.         printing := True;
  170.         PrintTheFile(theFile, FilesName);
  171.         PrintBtn.Caption := '&Print...';
  172.         printing := False;
  173.       end;
  174.     finally
  175.       TextList.Free;
  176.     end
  177.   end;
  178. end;
  179.  
  180. { .INI file stuff }
  181.  
  182. procedure TFileForm.FormClose(Sender: TObject;
  183.   var Action: TCloseAction);
  184. {-Write to .INI file}
  185. begin
  186.   if UpdateBox.Checked then
  187.     with TIniFile.Create(ChangeFileExt
  188.     (ExtractFilename(Application.ExeName),'.INI')) do
  189.     try
  190.       with SelectFileDlg do
  191.       begin
  192.         WriteString('DirectoryPath', 'FilePath', FilePath);
  193.         WriteString('FileTypes', 'FileTypes', FileListBox1.Mask);
  194.       end;
  195.       with FindWhatDlg do
  196.       begin
  197.         WriteString('Search Options', 'SearchItem', ComboBox1.Text);
  198.         WriteBool('Search Options', 'StartAtTop', StartTop.Checked);
  199.         WriteBool('Search Options', 'AnyCaseChars', AnyCase.Checked);
  200.         WriteBool('Search Options', 'WholeWordsOnly', WholeWords.Checked);
  201.       end;
  202.       with PRNformatDlg do
  203.       begin
  204.         WriteBool('Print Format', 'Formatted', HasTitle.Checked);
  205.         WriteBool('Print Format', 'LineNumbering', LineNumbering.Checked);
  206.         WriteBool('Print Format', 'LastPageFirst', LastPageFirst.Checked);
  207.         WriteBool('Print Format', 'AutoSetCPI', AutoWidth.Checked);
  208.         WriteInteger('Print Format', 'PageWidth', page_width);
  209.       end;
  210.     finally
  211.       Free;
  212.     end;
  213. end;
  214.  
  215. procedure TFileForm.CheckCmdLine;
  216. begin
  217.   if ParamCount > 0 then
  218.   begin
  219.     theFile := ParamStr(1);
  220.     try
  221.       SelectFileDlg.DirectoryListBox1.Directory :=
  222.         ExtractFilePath(theFile);
  223.       ShowSelection(theFile);
  224.       if ParamCount > 1 then
  225.         with ViewText do
  226.         begin
  227.           theTarget := ParamStr(2);
  228.           LoadFile(theFile, FDateStr);
  229.         end
  230.     except
  231.       ShowMessage('Unable to set Directory List Box to '+theFile);
  232.     end;
  233.   end;
  234. end;
  235.  
  236. procedure TFileForm.FormShow(Sender: TObject);
  237. {-Read from .INI file}
  238. var
  239.   mp, ic, ix: integer;
  240. begin
  241.   with TIniFile.Create(ChangeFileExt(ExtractFilename
  242.   (Application.ExeName), '.INI')) do
  243.   try
  244.     with SelectFileDlg do
  245.     begin
  246.       FilePath := ReadString('DirectoryPath', 'FilePath', '');
  247.       FileTypes := ReadString('FileTypes', 'FileTypes', '*.*');
  248.     end;
  249.     with FindWhatDlg do
  250.     begin
  251.       ComboBox1.Text := ReadString('Search Options', 'SearchItem', '');
  252.       StartTop.Checked := ReadBool('Search Options', 'StartAtTop', True);
  253.       AnyCase.Checked := ReadBool('Search Options', 'AnyCaseChars', True);
  254.       WholeWords.Checked := ReadBool('Search Options', 'WholeWordsOnly', False);
  255.     end;
  256.     with PRNformatDlg do
  257.     begin
  258.       HasTitle.Checked := ReadBool('Print Format', 'Formatted', True);
  259.       LineNumbering.Checked := ReadBool('Print Format',
  260.        'LineNumbering', True);
  261.       LastPageFirst.Checked := ReadBool('Print Format',
  262.        'LastPageFirst', False);
  263.       AutoWidth.Checked := ReadBool('Print Format',
  264.        'AutoSetCPI', False);
  265.       page_width := ReadInteger('Print Format', 'PageWidth', 80);
  266.     end;
  267.   finally
  268.     Free;
  269.   end;
  270.  
  271.   with SelectFileDlg do {set up Select File Dialog}
  272.   begin
  273.     if FilePath = '' then {get current}
  274.       GetDir(0, FilePath);
  275.     DirectoryListBox1.Directory := FilePath;
  276.     DriveComboBox1.Drive := DirectoryListBox1.Drive;
  277.     {Set selected item in FilterComboBox to stored FileTypes }
  278.     mp := pos('|'+FileTypes, FilterComboBox1.Filter);
  279.     if mp > 0 then {it's in the combo's filter list, so use it}
  280.     begin
  281.       FileListBox1.Mask := FileTypes;
  282.       {make combo selection match filelist at start}
  283.       ic := 0;
  284.       for ix := 1 to mp-1 do
  285.         if FilterComboBox1.Filter[ix] = '|' then
  286.           inc(ic);
  287.       FilterComboBox1.ItemIndex := ic div 2;
  288.     end;
  289.   end;
  290.  
  291.   {-Cmd line will override .INI}
  292.   CheckCmdLine;
  293. end;
  294.  
  295. { Drag and drop stuff }
  296.  
  297. procedure TFileForm.WndProc(var Msg: TMessage);
  298. {-Respond to DropFile message}
  299. var
  300.   buff: Array[0..80] of Char;
  301. begin
  302.   if Msg.Msg = WM_DropFiles then
  303.   begin
  304.     Msg.Result := 0;
  305.     DragQueryFile(Msg.wParam, 0, @buff, SizeOf(buff));
  306.     if StrLen(buff) <> 0 then
  307.     begin
  308.       theFile := StrPas(buff);
  309.       SetFocus;
  310.       ShowSelection(theFile);
  311.     end;
  312.     DragFinish(Msg.wParam);
  313.   end
  314.   else
  315.     inherited WndProc(Msg);
  316. end;
  317.  
  318. procedure TFileForm.FormDragDrop(Sender, Source: TObject; X, Y: Integer);
  319. {-Drop from file select dialog}
  320. begin
  321.   theFile := SelectFileDlg.FileSpec;
  322.   SelectFileDlg.Close;
  323.   SetFocus;
  324.   ShowSelection(theFile);
  325. end;
  326.  
  327. procedure TFileForm.FormDragOver(Sender, Source: TObject; X, Y: Integer;
  328.   State: TDragState; var Accept: Boolean);
  329. begin
  330.   Accept := Source is TFileListBox;
  331. end;
  332.  
  333. { DDE Stuff }
  334.  
  335. procedure TFileForm.DdeClientItem1Change(Sender: TObject);
  336. {-Get server data}
  337. var
  338.   F: string;
  339.   ps: Integer;
  340. begin
  341.   F := DdeClientItem1.Text;
  342.   { Data format is 'filename searchstr'}
  343.   ps := pos(' ', F);
  344.   if ps > 0 then {have valid data}
  345.   begin
  346.     theFile := copy(F, 1, ps-1);
  347.     { Removing DDE added spaces -- ok, but then your search
  348.       string can never have trailing spaces}
  349.     while F[Length(F)] = ' ' do {remove extraneous spaces}
  350.        System.Delete(F, Length(F), 1);
  351.     ViewText.theTarget := copy(F, ps+1, Length(F)-ps);
  352.     ShowSelection(theFile);
  353.     ViewText.LoadFile(theFile, FDateStr);
  354.   end;
  355. end;
  356.  
  357. procedure TFileForm.DDELinkBtnClick(Sender: TObject);
  358. const
  359.   Service = 'DELGREP';
  360.   Topic = 'Delphi Grep';
  361. begin
  362.   if DdeClientConv1.SetLink(Service, Topic) then
  363.   begin
  364.     DdeClientItem1.DdeItem := 'DdeServerItem1';
  365.     if DdeClientConv1.ConnectMode = ddeManual then
  366.       DdeClientConv1.OpenLink;
  367.     DDELinkBtn.Enabled := False;
  368.   end
  369.   else
  370.     ShowMessage('Unable to create DDE Link to '+Service);
  371. end;
  372.  
  373. end.
  374.